using ImGuiNET;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;

namespace AssetImporter
{
    public interface IUI
    {
        void Logic(double deltaSeconds);
        void Render();
    }

    public class UI : IUI
    {
        readonly IInputHandle inputHandle;
        readonly ImGuiController handle;
        readonly UIInner ui;
        int width;
        int height;
        public UI(int width, int height, IInputHandle inputHandle)
        {
            this.inputHandle = inputHandle;
            this.width = width;
            this.height = height;
            handle = new ImGuiController(width, height);
            ui = new UIInner();

            Importer.window.OnWindowReset += OnResize;
            inputHandle.OnTextInput += OnTextInput;
            inputHandle.OnMouseWheel += OnMouseWheel;
        }

        void OnResize(int width, int height)
        {
            this.width = width;
            this.height = height;
            handle.WindowResized(width, height);
        }

        public void Logic(double deltaSeconds)
        {
            handle.Update(Importer.window, (float)deltaSeconds);
            ui.logic(deltaSeconds);
        }

        public void Render()
        {
            handle.Render();
            ImGuiController.CheckGLError("End of frame");
        }

        void OnTextInput(int unicodeChar)
        {
            handle.PressChar((char)unicodeChar);
        }

        void OnMouseWheel(float x, float y)
        {
            handle.MouseScroll(x, y);
        }
    }

    class UIInner
    {
        bool showSubWindow = false;
        public void logic(double deltaSeconds)
        {
            ShowMainMenuBar(deltaSeconds);
            if(showSubWindow)
                ShowSubWindow(deltaSeconds);
        }

        void ShowMainMenuBar(double deltaSeconds)
        {
            if (ImGui.BeginMainMenuBar())
            {
                if (ImGui.BeginMenu("文件"))
                {
                    if (ImGui.MenuItem("退出", "ESC"))
                        Importer.orderManager.Add(new QuitOrder());
                    ImGui.EndMenu();
                }
                if (ImGui.BeginMenu("窗口"))
                {
                    ImGui.MenuItem("子窗口测试", null, ref showSubWindow);
                    ImGui.Separator();
                    if (ImGui.MenuItem("Cut", "CTRL+X")) {}
                    if (ImGui.MenuItem("Copy", "CTRL+C")) {}
                    if (ImGui.MenuItem("Paste", "CTRL+V")) {}
                    ImGui.EndMenu();
                }
                ImGui.EndMainMenuBar();
            }
        }

        static Vector3 BaseCamePos = new Vector3(5, 0, 5);
        Vector2 xy = new Vector2();
        void ShowSubWindow(double deltaSeconds)
        {
            ImGui.Begin("子窗口", ref showSubWindow, ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.AlwaysAutoResize);
            ImGui.Text("FPS: " + 1/ deltaSeconds);
            ImGui.DragFloat("x", ref xy.X, 1, -10, 10);
            ImGui.DragFloat("y", ref xy.Y, 1, -10, 10);

            Importer.orderManager.Add(new CameraSurrendTargetOrder(xy));
            ImGui.End();
        }
    }
}
